import { DbConfig } from "../../config/DbConfig";
import { Logger } from "./Logger";
import { MongodbClient } from "./MongodbClient";

export class MongodbManager{

    public static instance: MongodbManager = new MongodbManager();

    public dbMap: Map<string, MongodbClient> = new Map<string, MongodbClient>();
    
    public async init(){
        
    }

    public async createClient(dbName: string) {
        let dbConfig: DbConfig = DbConfig.getDbConfig(DbConfig.now_db_type)
        let client: MongodbClient = await MongodbClient.create(dbName, dbConfig.host, dbConfig.port, dbConfig.user, dbConfig.pass);
        this.dbMap.set(dbName, client);
        return client;
    }

    public async getDb(dbName: string) {
        let db: MongodbClient;
        if (this.dbMap.has(dbName)) {
            db = this.dbMap.get(dbName);
        } else {
            let dbType: number = DbConfig.now_db_type;
            Logger.info("数据库client", dbName, dbType);
            db = await this.createClient(dbName);
        }
        return db;
    }

}